-- ========================================================================
-- northwind database example queries
-- ========================================================================

------------------------------------------------------------------------
-- Basic graph traversals (single hop)
------------------------------------------------------------------------

-- List all products in the "Seafood" category.
SELECT p.product_name
FROM MATCH (p:product)-[:part_of]->(c:category)
WHERE c.category_name = 'Seafood';

-- Which products are supplied by suppliers based in Germany?
SELECT s.company_name, p.product_name
FROM MATCH (s:supplier)-[:supplies]->(p:product)
WHERE s.country = 'Germany';

-- Which territories belong to the "Eastern" region?
SELECT t.territory_description
FROM MATCH (t:territory)-[:in_region]->(r:region)
WHERE r.region_description = 'Eastern';

-- Which sales territories does employee "Robert King" cover?
SELECT e.first_name, e.last_name, t.territory_description
FROM MATCH (e:employee)-[:in_territory]->(t:territory)
WHERE e.last_name = 'King';

-- Who is the manager of employee "Robert King"?
SELECT e.first_name AS employee_first, e.last_name AS employee_last,
       m.first_name AS manager_first, m.last_name AS manager_last
FROM MATCH (e:employee)-[:reports_to]->(m:employee)
WHERE e.last_name = 'King';

------------------------------------------------------------------------
-- Graph analytics (aggregate / business functions)
------------------------------------------------------------------------

-- Which product categories generate the most sales revenue?
SELECT c.category_name,
       SUM(oe.unit_price * oe.quantity * (1 - oe.discount)) AS revenue
FROM MATCH (o:`order`)-[oe:orders]->(p:product),
  (p:product)-[:part_of]->(c:category)
GROUP BY c.category_name
ORDER BY revenue DESC;

-- What are the top 10 products by total quantity sold?
SELECT p.product_name, SUM(oe.quantity) AS totalQty
FROM MATCH (o:`order`)-[oe:orders]->(p:product)
GROUP BY p.product_name
ORDER BY totalQty DESC
LIMIT 10;

-- How many orders has each shipper delivered?
SELECT sh.company_name, COUNT(o) AS numOrders
FROM MATCH (sh:shipper)-[:ships]->(o:`order`)
GROUP BY sh.company_name
ORDER BY numOrders DESC;

-- How much sales revenue has each employee generated?
SELECT e.first_name, e.last_name,
       SUM(oe.unit_price * oe.quantity * (1 - oe.discount)) AS revenue
FROM MATCH (e:employee)-[:sold]->(o:`order`),
  (o:`order`)-[oe:orders]->(p:product)
GROUP BY e.first_name, e.last_name
ORDER BY revenue DESC;

-- How many products does each supplier provide? (top 10)
SELECT s.company_name, COUNT(p) AS numProducts
FROM MATCH (s:supplier)-[:supplies]->(p:product)
GROUP BY s.company_name
ORDER BY numProducts DESC
LIMIT 10;

-- For each category, how many products and what is their average unit price?
SELECT c.category_name, COUNT(p) AS numProducts, AVG(p.unit_price) AS avgPrice
FROM MATCH (p:product)-[:part_of]->(c:category)
GROUP BY c.category_name
ORDER BY avgPrice DESC;

-- What is the total freight cost of orders, grouped by customer country?
SELECT cu.country, SUM(o.freight) AS totalFreight
FROM MATCH (cu:customer)-[:purchased]->(o:`order`)
GROUP BY cu.country
ORDER BY totalFreight DESC;

-- What is the average discount given on products in each category?
SELECT c.category_name, AVG(oe.discount) AS avgDiscount
FROM MATCH (o:`order`)-[oe:orders]->(p:product),
  (p:product)-[:part_of]->(c:category)
GROUP BY c.category_name
ORDER BY avgDiscount DESC;

------------------------------------------------------------------------
-- Multi-pattern graph queries (joins across relationships)
------------------------------------------------------------------------

-- Which customers have bought products in the "Beverages" category?
SELECT DISTINCT cu.company_name
FROM MATCH (cu:customer)-[:purchased]->(o:`order`),
  (o:`order`)-[:orders]->(p:product),
  (p:product)-[:part_of]->(c:category)
WHERE c.category_name = 'Beverages';

-- Which suppliers provide products in the "Confections" category?
SELECT DISTINCT s.company_name
FROM MATCH (s:supplier)-[:supplies]->(p:product),
  (p:product)-[:part_of]->(c:category)
WHERE c.category_name = 'Confections';

-- Which employees have sold to customers in the USA?
SELECT DISTINCT e.first_name, e.last_name
FROM MATCH (e:employee)-[:sold]->(o:`order`),
  (o:`order`)<-[:purchased]-(cu:customer)
WHERE cu.country = 'USA';

-- Which employees report to "Steven Buchanan"?
SELECT e.first_name, e.last_name
FROM MATCH (e:employee)-[:reports_to]->(m:employee)
WHERE m.last_name = 'Buchanan';

-- Which customer countries does each shipper deliver to?
SELECT DISTINCT sh.company_name, cu.country
FROM MATCH (sh:shipper)-[:ships]->(o:`order`),
  (o:`order`)<-[:purchased]-(cu:customer)
ORDER BY sh.company_name, cu.country;

-- Which employees sold the product "Chai", and how many units each?
SELECT e.first_name, e.last_name, SUM(oe.quantity) AS units
FROM MATCH (e:employee)-[:sold]->(o:`order`),
  (o:`order`)-[oe:orders]->(p:product)
WHERE p.product_name = 'Chai'
GROUP BY e.first_name, e.last_name
ORDER BY units DESC;

-- Who are the top 5 customers by total order value?
SELECT cu.company_name,
       SUM(oe.unit_price * oe.quantity * (1 - oe.discount)) AS totalValue
FROM MATCH (cu:customer)-[:purchased]->(o:`order`),
  (o:`order`)-[oe:orders]->(p:product)
GROUP BY cu.company_name
ORDER BY totalValue DESC
LIMIT 5;

------------------------------------------------------------------------
-- Variable-length paths and shortest path (advanced graph)
------------------------------------------------------------------------

-- Show the full management chain above employee "Robert King".
SELECT m.first_name, m.last_name
FROM MATCH (e:employee)-[:reports_to{1,3}]->(m:employee)
WHERE e.last_name = 'King';

-- Which products are most often ordered together with "Chai"?
SELECT p2.product_name, COUNT(o) AS timesTogether
FROM MATCH (p1:product)<-[:orders]-(o:`order`),
  (o:`order`)-[:orders]->(p2:product)
WHERE p1.product_name = 'Chai'
GROUP BY p2.product_name
ORDER BY timesTogether DESC
LIMIT 10;

-- Find pairs of products that were bought together in the same order.
SELECT DISTINCT p1.product_name, p2.product_name
FROM MATCH (p1:product)<-[:orders]-(o:`order`),
  (o:`order`)-[:orders]->(p2:product)
WHERE p1.product_id < p2.product_id;

-- Find the shortest reporting connection between "Robert King" and "Nancy Davolio".
SELECT k.first_name, k.last_name, n.first_name, n.last_name
FROM MATCH ANY_SHORTEST (k:employee)-[e:reports_to{1,4}]-(n:employee)
WHERE k.last_name = 'King' AND n.last_name = 'Davolio';

-- Which other customers have bought at least one of the same products as "Ernst Handel"?
SELECT DISTINCT c2.company_name
FROM MATCH
  (c1:customer)-[:purchased]->(o1:`order`),
  (o1:`order`)-[:orders]->(p:product),
  (p:product)<-[:orders]-(o2:`order`),
  (o2:`order`)<-[:purchased]-(c2:customer)
WHERE c1.company_name = 'Ernst Handel'
AND c1 <> c2;


